home *** CD-ROM | disk | FTP | other *** search
/ Future Workshop / Future Workshop.iso / multimed / qtw111 / mplayer / playmain.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  16KB  |  486 lines

  1.  
  2. // ---------------------------------------------------------------------
  3. //
  4. // PlayMain.c - Movie Player - QuickTime for Windows
  5. //
  6. //              Version 1.0
  7. //
  8. //              (c) 1988-1992 Apple Computer, Inc. All Rights Reserved.
  9. //
  10. // ---------------------------------------------------------------------
  11.  
  12.  
  13. // Includes
  14. // --------
  15. #include <Windows.h> // Required by Windows
  16.  
  17. #include <qtw.h>   // Interface to QuickTime
  18. #include <qtole.h> // Interface to qtole dll's
  19.  
  20. #include "common.h" // Interface to common.c routines
  21.  
  22. #include "player.h"  // Interface to other *.c files
  23. #include "player.hr" // Defines used in *.rc files
  24.  
  25.  
  26. // Message-Persistent Data
  27. // -----------------------
  28. static struct // Hungarian notation: g
  29.   {HINSTANCE      hInstance;          // Instance handle
  30.    HINSTANCE      hResources;         // Resource-only DLL handle
  31.    HWND           hwndFrame;          // Frame window handle
  32.    HWND           hwndClient;         // MDI client window
  33.    HMENU          hMenu;              // Frame window menu
  34.    HACCEL         hAccel;             // Frame window accelerators
  35.    HWND           hActiveModelessDlg; // Handle of active modeless dlg if any
  36.    QTOLE_OLEDATA  qtoleOleData;       // OLE data struct
  37.  
  38.   } g;
  39.  
  40.  
  41. // Internal Function Declarations
  42. // ------------------------------
  43. static LPSTR NEAR PlayerParseCmdLine    (LPSTR);
  44. static  BOOL NEAR PlayerInitAppl        (VOID);
  45. static  HWND NEAR PlayerInitInst        (HINSTANCE, LPSTR, int);
  46. static  LONG NEAR PlayerTerminateInst   (VOID);
  47. static  BOOL NEAR DoQuickTimeInit       (HINSTANCE, LPSTR, LPINT);
  48. static  VOID NEAR KillQuickTime         (VOID);
  49.  
  50.  
  51. // Function: WinMain - Required Windows "Main" Routine
  52. // --------------------------------------------------------------------
  53. // Parameters: As required by Microsoft Windows
  54. //
  55. // Returns:    As required by Microsoft Windows
  56. // --------------------------------------------------------------------
  57. int PASCAL WinMain (HINSTANCE hInstance,
  58.                  HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  59.  
  60. // Define function data
  61.  
  62. {
  63.     MSG  msg; // Message
  64.  
  65.     g.hInstance = hInstance; // Initialize global data
  66.  
  67.     // Load resource-only DLL
  68.     if( !(g.hResources = CommonGetLocalizedResources
  69.         ( PLAYER_ROOT_NAME, hInstance, PLAYER_STRING_NOMEMORY )))
  70.         return 0;
  71.  
  72.     // Perform one-time initialization
  73.     if ( !hPrevInstance && !PlayerInitAppl() )
  74.         return 0;
  75.  
  76.     // Perform initializations that apply to a specific instance and create
  77.     // create window
  78.     if ( !PlayerInitInst( hPrevInstance, lpCmdLine, nCmdShow ))
  79.         return 0;
  80.  
  81.     // Main message loop
  82.     while (GetMessage (&msg, NULL, NULL, NULL)) {
  83.         if( !g.hActiveModelessDlg ||
  84.             !IsDialogMessage( g.hActiveModelessDlg, &msg )) {
  85.             if( !g.hwndClient ||
  86.                 !TranslateMDISysAccel( g.hwndClient, &msg )) {
  87.                 if( !g.hwndFrame ||
  88.                     !TranslateAccelerator( g.hwndFrame, g.hAccel, &msg )) {
  89.                     TranslateMessage (&msg);
  90.                     DispatchMessage  (&msg);
  91.                 }
  92.             }
  93.         }
  94.     }
  95.  
  96.     // Cleanup movie player
  97.  
  98.     PlayerTerminateInst();
  99.  
  100.     return msg.wParam;
  101. }
  102.  
  103.  
  104. // Function: PlayerInitAppl - Perform One-time Initialization
  105. // --------------------------------------------------------------------
  106. // Parameters: None
  107. //
  108. // Returns:    TRUE if OK, else FALSE
  109. // --------------------------------------------------------------------
  110. static BOOL NEAR PlayerInitAppl ( VOID )
  111.  
  112. {
  113.     WNDCLASS wc; // Window class information
  114.  
  115.     // Register the frame (main) window class
  116.  
  117.     wc.style         = 0;
  118.     wc.lpfnWndProc   = PlayerFrameWndProc;
  119.     wc.cbClsExtra    = 0;
  120.     wc.cbWndExtra    = 0;
  121.     wc.hInstance     = g.hInstance;
  122.     wc.hIcon         = LoadIcon( g.hResources,
  123.         MAKEINTRESOURCE( PLAYER_PLAYER_ICON ));
  124.     wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  125.     wc.hbrBackground = (HBRUSH) ( COLOR_APPWORKSPACE + 1 );
  126.     wc.lpszMenuName  = NULL;
  127.     wc.lpszClassName = PLAYER_FRAME_CLASS;
  128.  
  129.     if( !RegisterClass( &wc ))
  130.         return FALSE;
  131.  
  132.     // Register the movie window class
  133.  
  134.     wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  135.     wc.lpfnWndProc   = PlayerMovieWndProc;
  136.     wc.cbClsExtra    = 0;
  137.     wc.cbWndExtra    = sizeof( VOID NEAR * );
  138.     wc.hInstance     = g.hInstance;
  139.     wc.hIcon         = NULL;
  140.     wc.hCursor       = NULL; // Set to NULL so we can set cursor in the wndproc
  141.     wc.hbrBackground = (HBRUSH) NULL;
  142.     wc.lpszMenuName  = NULL;
  143.     wc.lpszClassName = PLAYER_MOVIE_CLASS;
  144.  
  145.     return RegisterClass( &wc );
  146. }
  147.  
  148.  
  149. // Function: PlayerInitInst - Perform Instance Initialization
  150. // --------------------------------------------------------------------
  151. // Parameters: HINSTANCE hPrevInstance;  Previous instance
  152. //             LPSTR     lpCmdLine;      -->Command line arguments
  153. //             int       nCmdShow;       Parameter for first ShowWindow()
  154. //
  155. // Returns:    HWND      hwndFrame;      Frame window handle
  156. //                                       or NULL if initialization failed
  157. // --------------------------------------------------------------------
  158. static HWND NEAR PlayerInitInst
  159.               ( HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  160.  
  161. {
  162.     // Do QuickTime Initializations.
  163.     if( !DoQuickTimeInit( hPrevInstance, lpCmdLine, &nCmdShow ) )
  164.         return NULL;
  165.  
  166.     // get menu and accelerators from localized resource
  167.     g.hAccel = LoadAccelerators ( g.hResources,
  168.         MAKEINTRESOURCE( PLAYER_ACCELERATORS ));
  169.     g.hMenu  = LoadMenu( g.hResources,
  170.         MAKEINTRESOURCE( PLAYER_FRAME_MENU ));
  171.     if( !g.hAccel || !g.hMenu ) {
  172.         CommonTellUser( g.hResources, PLAYER_STRING_NOACCELORMENU,
  173.             PLAYER_STRING_CAPTION, MB_OK );
  174.         return NULL;
  175.     }
  176.  
  177.     // Create a main window for this application instance.
  178.     if ( !(g.hwndFrame = CreateWindow( PLAYER_FRAME_CLASS, "",
  179.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  180.         CW_USEDEFAULT, CW_USEDEFAULT,
  181.         CW_USEDEFAULT, CW_USEDEFAULT,
  182.         NULL, g.hMenu, g.hInstance, NULL ))) {
  183.         CommonTellUser( g.hResources, PLAYER_STRING_NOWINDOW,
  184.             PLAYER_STRING_CAPTION, MB_OK );
  185.         return NULL;
  186.     }
  187.  
  188.     // Tell qtole.dll the server hwnd
  189.     QTOLE_SetApplicationHwnd( &g.qtoleOleData, g.hwndFrame ); 
  190.  
  191.     // get MDI client window created during WM_CREATE message processing
  192.     g.hwndClient = PlayerQueryClientWindow(); // this is in FrameWnd.c
  193.  
  194.     // Display the frame window
  195.     // If ole started the app, the window will be initially hidden
  196.     ShowWindow  ( g.hwndFrame, nCmdShow );
  197.     if( nCmdShow != SW_HIDE )
  198.         UpdateWindow( g.hwndFrame );
  199.  
  200.     // Check command line for movie file.
  201.     // Note: this must come after ShowWindow, UpdateWindow
  202.     if( lpCmdLine = PlayerParseCmdLine( lpCmdLine ))
  203.         SendMessage( g.hwndFrame, WM_PLAYER_CMDLINE,
  204.         0, (LPARAM) lpCmdLine );
  205.  
  206.     return g.hwndFrame;
  207. }
  208.  
  209. // Function: PlayerParseCmdLine - Parse the command line. Return -> to
  210. //                                first argument and ignore any extras.
  211. //                                The default extension is appended if
  212. //                                name has no extension
  213. // --------------------------------------------------------------------
  214. // Parameters: LPSTR      lpCmdLine     command line pointer
  215. //
  216. // Returns:    LPSTR      lpCmdLine     command line pointer to first
  217. //                                      argument, else NULL
  218. // --------------------------------------------------------------------
  219. static LPSTR NEAR PlayerParseCmdLine( LPSTR lpCmdLine )
  220.  
  221. {
  222.     LPSTR  lpTemp;                        // Temp pointer
  223.     char   szExtension[FILE_EXT_LEN + 1]; // Default file extension
  224.     BOOL   bExtension;                    // Extension flag
  225.  
  226.     // Command line is already in Ansi char set even if entered from DOS
  227.     // command line
  228.  
  229.     // remove any leading blanks
  230.     while( *lpCmdLine == ' ' )
  231.         lpCmdLine = AnsiNext( lpCmdLine );
  232.  
  233.     if( *lpCmdLine == '\0' )
  234.         return NULL;
  235.  
  236.     // look for blank or end of string
  237.     bExtension = FALSE;
  238.     lpTemp = lpCmdLine;
  239.     while( *lpTemp && (*lpTemp != ' ' )) {
  240.         if( *lpTemp == '.' )
  241.             bExtension = TRUE;
  242.         lpTemp = AnsiNext( lpTemp );
  243.     }
  244.     *lpTemp = '\0';
  245.  
  246.     if( !bExtension ) {
  247.         LoadString( g.hResources,
  248.             PLAYER_STRING_FILEEXT, szExtension, sizeof( szExtension ));
  249.         lstrcat( lpCmdLine, szExtension );
  250.     }
  251.  
  252.     return lpCmdLine;
  253. }
  254.  
  255.  
  256. // Function: PlayerTerminateInst - Terminate Instance
  257. // --------------------------------------------------------------------
  258. // Parameters: VOID
  259. //
  260. // Returns:    Always 0L
  261. // --------------------------------------------------------------------
  262. static LONG NEAR PlayerTerminateInst( VOID )
  263.  
  264. {
  265.     // Clean up OLE
  266.     if( g.qtoleOleData.lpqtoleServer )
  267.         QTOLE_OLECleanUp( &g.qtoleOleData );
  268.  
  269.     // Cut the connections to QuickTime.
  270.     KillQuickTime();
  271.  
  272.     // Free the resource-only DLL
  273.     if ( g.hResources && ( g.hInstance != g.hResources ))
  274.         FreeLibrary( g.hResources );
  275.  
  276.     return 0L;
  277. }
  278.  
  279.  
  280. // The next two functions are used to initialize and kill QuickTime
  281.  
  282. // Function: DoQuickTimeInit - Establishes connections to QuickTime
  283. // --------------------------------------------------------------------
  284. // Parameters: HINSTANCE  hPrevInstance  Previous instance
  285. //             LPSTR      lpCmdLine      -> command line
  286. //             LPINT      lpnCmdShow     Parameter for first ShowWindow()
  287. //
  288. // Returns:    BOOL       TRUE if OK, else FALSE
  289. // --------------------------------------------------------------------
  290. static BOOL NEAR DoQuickTimeInit
  291.             ( HINSTANCE hPrevInstance, LPSTR lpCmdLine, LPINT lpnCmdShow )
  292.  
  293. {
  294.     OSErr       oserr;       // Temp return value
  295.     WORD        wIDString;   // ID of error message string
  296.     int         cQueue = 64; // Best queue size
  297.     QTOLE_INIT  qtoleInit;
  298.     QTOLE_ERR   qtole_err;
  299.     char        szCaption[30];
  300.  
  301.  
  302.     LoadString( g.hResources, PLAYER_STRING_CAPTION,
  303.         szCaption, sizeof( szCaption ));
  304.  
  305.     // Do OLE initialization before QTInitialize so we
  306.     // don't have to kill qt for install only
  307.     if( !( qtoleInit.fpServerCallBack = (QTOLEPROC) MakeProcInstance( 
  308.         (FARPROC) QTOLEServerCallBack, g.hInstance ))) {
  309.         CommonTellUser( g.hResources, PLAYER_STRING_NOMEMORY,
  310.             PLAYER_STRING_CAPTION, MB_OK );
  311.         return FALSE;
  312.     }
  313.     else {
  314.         qtoleInit.lStructSize           = sizeof( qtoleInit );
  315.         qtoleInit.lVersion              = VERSION_1;
  316.         qtoleInit.hInstance             = g.hInstance;
  317.         qtoleInit.hResources            = g.hResources;
  318.         qtoleInit.lpCmdLine             = lpCmdLine;
  319.         qtoleInit.lpClassName           = PLAYER_FRAME_CLASS;
  320.         qtoleInit.lpServerCaption       = szCaption;
  321.         qtoleInit.lpnCmdShow            = lpnCmdShow;
  322.         qtoleInit.wIDFirstString        = OLE_STRING_FIRST;
  323.         qtoleInit.wIDFirstDlg           = OLE_DLG_FIRST;
  324.         qtoleInit.bMultipleObjectServer = TRUE;
  325.  
  326.         g.qtoleOleData.lStructSize      = sizeof( g.qtoleOleData );
  327.         g.qtoleOleData.lVersion         = VERSION_1;
  328.         g.qtoleOleData.wObjectType      = MOVIE_OBJECT;
  329.  
  330.         if( QTOLE_OK != 
  331.             ( qtole_err = QTOLE_Initialize( &g.qtoleOleData, &qtoleInit ))) {
  332.             if( QTOLE_INSTALL_ONLY != qtole_err )
  333.                 CommonTellUser( g.hResources, 
  334.                 PLAYER_STRING_OLEINITFAILED,
  335.                 PLAYER_STRING_CAPTION, MB_OK );
  336.             return FALSE;
  337.         }
  338.     }
  339.  
  340.  
  341.     // Enlarge message queue to prevent occasional lose of posted messages
  342.     while( SetMessageQueue( cQueue-- ) == 0 );
  343.  
  344.     if( ( oserr = QTInitialize( NULL )) != QTI_OK ) {
  345.         switch( oserr ) {
  346.             case QTI_FAIL_NOEXIST:
  347.                 wIDString = PLAYER_STRING_QTWNOEXIST;
  348.                 break;
  349.  
  350.             case QTI_FAIL_CORRUPTDLL:
  351.                 wIDString = PLAYER_STRING_QTWBADDLL;
  352.                 break;
  353.  
  354.             case QTI_FAIL_286:
  355.                 wIDString = PLAYER_STRING_QTW286;
  356.                 break;
  357.  
  358.             case QTI_FAIL_WIN30:
  359.                 wIDString = PLAYER_STRING_QTWWIN30;
  360.                 break;
  361.  
  362.             default:
  363.                 wIDString = PLAYER_STRING_QTWFAILED;
  364.                 break;
  365.         }
  366.  
  367.         CommonTellUser( g.hResources, wIDString,
  368.             PLAYER_STRING_CAPTION, MB_OK );
  369.         return FALSE;
  370.     }
  371.  
  372.     if( EnterMovies() ) {
  373.         CommonTellUser( g.hResources, PLAYER_STRING_ENTMOVFAILED,
  374.             PLAYER_STRING_CAPTION, MB_OK );
  375.         return FALSE;
  376.     }
  377.  
  378.     return TRUE;
  379. }
  380.  
  381. // Function: KillQuickTime - Cuts the connections to QuickTime
  382. // --------------------------------------------------------------------
  383. // Parameters: VOID
  384. //
  385. // Returns:    VOID
  386. // --------------------------------------------------------------------
  387. static VOID NEAR KillQuickTime( VOID )
  388.  
  389. {
  390.     ExitMovies();
  391.     QTTerminate();
  392.  
  393.     return;
  394. }
  395.  
  396.  
  397. //  This function is called by other modules whenever a modeless dialog
  398. //  is activated or deactivated
  399.  
  400. // Function: PlayerSetActiveModeless - Set handle of active modeless dlg
  401. // --------------------------------------------------------------------
  402. // Parameters: WPARAM   wParam          WA_ flag defined in Windows.h
  403. //             HWND     hModelessDlg    Handle of dlg being activated or
  404. //                                      deactivated
  405. //
  406. // Returns:    VOID
  407. // --------------------------------------------------------------------
  408. VOID FAR PlayerSetActiveModeless( WPARAM wParam, HWND hModelessDlg )
  409.  
  410. {
  411.     if( wParam != WA_INACTIVE )
  412.         g.hActiveModelessDlg = hModelessDlg;
  413.     else if( g.hActiveModelessDlg == hModelessDlg )
  414.         g.hActiveModelessDlg = NULL;
  415.  
  416.     return;
  417. }
  418.  
  419. // Function: PlayerNoMoreWindow - Sets global handles to NULL. Called during
  420. //                                frame window WM_DESTROY message processing
  421. // --------------------------------------------------------------------
  422. // Parameters: VOID
  423. //
  424. // Returns:    VOID
  425. // --------------------------------------------------------------------
  426. VOID FAR PlayerNoMoreWindow( VOID )
  427.  
  428. {
  429.     g.hwndFrame  = NULL;
  430.     g.hwndClient = NULL;
  431.  
  432.     return;
  433. }
  434.  
  435. ///  The remaining functions are the query functions called by other modules
  436.  
  437. // Function: PlayerQueryInstance - Query Instance Handle
  438. // --------------------------------------------------------------------
  439. // Parameters: None.
  440. //
  441. // Returns:    HINSTANCE hInstance;    Application instance handle
  442. // --------------------------------------------------------------------
  443. HINSTANCE FAR PlayerQueryInstance( VOID )
  444.  
  445. {
  446.     return g.hInstance;
  447. }
  448.  
  449. // Function: PlayerQueryResources - Query Resource-Only DLL Handle
  450. // --------------------------------------------------------------------
  451. // Parameters: None.
  452. //
  453. // Returns:    HINSTANCE hResources    Resource-only DLL handle
  454. // --------------------------------------------------------------------
  455. HINSTANCE FAR PlayerQueryResources( VOID )
  456.  
  457. {
  458.     return g.hResources;
  459. }
  460.  
  461. // Function: PlayerQueryFrameWindow - Query Frame Window Handle
  462. // --------------------------------------------------------------------
  463. // Parameters: None.
  464. //
  465. // Returns:    HWND hwndFrame;          Frame window handle
  466. // --------------------------------------------------------------------
  467. HWND FAR PlayerQueryFrameWindow( VOID )
  468.  
  469. {
  470.     return g.hwndFrame;
  471. }
  472.  
  473. // Function: PlayerQueryOleData - Query -> to Ole Data struct
  474. // --------------------------------------------------------------------
  475. // Parameters: None.
  476. //
  477. // Returns:    LPQTOLE_OLEDATA        -> ole data struct
  478. // --------------------------------------------------------------------
  479. LPQTOLE_OLEDATA FAR PlayerQueryOleData( VOID )
  480.  
  481. {
  482.     return &g.qtoleOleData;
  483. }
  484.  
  485.  
  486.